Sorting Algorithms
1 min readRapid overview
Sorting Algorithms (Frontend Focus)
Use this sheet to explain trade-offs quickly and reference JavaScript examples.
Common algorithms
| Algorithm | Avg Time | Worst Time | Space | Stable | Notes |
|---|---|---|---|---|---|
| Quick sort | O(n log n) | O(n^2) | O(log n) | ❌ | Fast average, not stable |
| Merge sort | O(n log n) | O(n log n) | O(n) | ✅ | Stable, good for large arrays |
| Heap sort | O(n log n) | O(n log n) | O(1) | ❌ | In-place, not stable |
JavaScript notes
Array.prototype.sortis stable in modern engines.- Always provide a comparator for numeric sorting.
numbers.sort((a, b) => a - b);
Interview prompt
- Why is comparator-based sorting safer than default sort?